package com.qxy.hgz.douyinapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import android.widget.Toast;

import com.bytedance.sdk.open.aweme.CommonConstants;
import com.bytedance.sdk.open.aweme.authorize.model.Authorization;
import com.bytedance.sdk.open.aweme.common.handler.IApiEventHandler;
import com.bytedance.sdk.open.aweme.common.model.BaseReq;
import com.bytedance.sdk.open.aweme.common.model.BaseResp;
import com.bytedance.sdk.open.aweme.share.Share;
import com.bytedance.sdk.open.douyin.DouYinOpenApiFactory;
import com.bytedance.sdk.open.douyin.api.DouYinOpenApi;
import com.qxy.hgz.rank.ui.RankListActivity;
import com.qxy.hgz.util.ConfigInfo;
import com.qxy.hgz.util.InternetHandler;
import com.qxy.hgz.util.JsonUtil;
import com.qxy.hgz.util.RequestUtil;
import com.qxy.hgz.util.StorageUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Response;

/**
 * 主要功能：接受授权返回结果的activity
 * <p>
 * <p>
 * 也可通过request.callerLocalEntry = "com.xxx.xxx...activity"; 定义自己的回调类
 */
public class DouYinEntryActivity extends Activity implements IApiEventHandler {
    private static String authCode;
    private String accessToken = StorageUtil.getAccessToken();
    private String clientToken = StorageUtil.getClientToken();
    DouYinOpenApi douYinOpenApi;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        douYinOpenApi = DouYinOpenApiFactory.create(this);
        douYinOpenApi.handleIntent(getIntent(), this);
    }


    @Override
    public void onReq(BaseReq req) {

    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == CommonConstants.ModeType.SHARE_CONTENT_TO_TT_RESP) {
            Share.Response response = (Share.Response) resp;
            Toast.makeText(this, " code：" + response.errorCode + " 文案：" + response.errorMsg, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(this, RankListActivity.class);
            startActivity(intent);

        } else if (resp.getType() == CommonConstants.ModeType.SEND_AUTH_RESPONSE) {
            Authorization.Response response = (Authorization.Response) resp;
            Intent intent = new Intent(this, RankListActivity.class);
            if (resp.isSuccess()) {
                Log.d("xixi", "onResp: 开始解析" );
//                Toast.makeText(this, "授权成功，获得权限：" + response.authCode,
//                        Toast.LENGTH_LONG).show();
                authCode = response.authCode;
                RequestUtil.getAccessToken(ConfigInfo.MAIN_URL + "/oauth/access_token/",
                        authCode, response1 -> {
                            Log.d("xixi", "onResp: 进行解析" );
                            JSONObject json = new JSONObject(response1.body().string());
                            if (JsonUtil.isSuccessed(json)) {
                                Log.d("xixi", "access: " + json);
                            }else{
                                Log.d("xixi", "fail to get");
                            }

                        });
            }

        }
        finish();

    }

    @Override
    public void onErrorIntent(Intent intent) {
        // 错误数据
        Toast.makeText(this, "Intent出错", Toast.LENGTH_LONG).show();
    }



}
